<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/globalconfig.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/public/Products.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/public/Attachment.php";
$product = new Products();
$product->loginCheck();
$action = $_REQUEST["action"];
$attach = new Attachment();
$condition = array();

//$data = array();
$reda;
$message = "success";
$code = 200;
try {
    switch ($action) {
        case LOAD:
            $reda = $product->findOne();
            break;
        case LOAD_ID:
            $condition[$product->getPrimaryKey()] = $_GET["id"];
            $reda = $product->findOne($condition);
            error_log($reda);
            $attDatas = $attach->findList(["business_no" => $reda["dataNo"]]);
            $arr = array();
            foreach ($attDatas as $key => $val) {
                $arr[] = ["name" => $val["fileName"], "url" => $val["downloadUrl"],"idAttachment"=>$val["idAttachment"]];
            }
            $reda["files"] = $arr;
            break;
        case UPLOAD:
            echo "upload";
            break;
        case ALL:
            $reda = $product->findList();
            break;
        case PAGE:
            $page = $_REQUEST["currentPage"];
            $pageSize = $_REQUEST["pageSize"];
            $condition = $product->searchData();
            $reda = $product->findByPage($page, $pageSize, $condition);
            break;
        case DELETE:
            $ids = $_POST["ids"];
            $condition[$product->getPrimaryKey()] = [$product->param_operation => "in", $product->param_value_key => $ids];

            $reda = $product->delete($condition);
            break;
        case MERGE:
            $data = $product->parseRequestData();
            $reda = $product->merge($product->getPrimaryKey(), $data);
            $attach->upload($reda,"product");
            break;
        default:
            $reda = "未知Action";
    }
    $product->result($reda, $message, $code);
} catch (Exception $e) {
    $product->result($e->getMessage(), $product->error_msg, $product->error_code);
}


?>
